import React from 'react'
import {useSelector,useDispatch} from 'react-redux'
import {incrementActionCreator} from '../store/actions/counterAction'

export default function Counter() {
  /*
    useSelector的参数是一个回调函数，
    回调函数的参数就是store中的state
    返回值就是return出来的数据
  */
  const count=useSelector((state)=>{
    return state.num
  })

  //创建dipatch对象
  const dispatch=useDispatch()
  const increment=(n)=>{
    dispatch(incrementActionCreator(n))
  }
  return (
    <div>
      <h1>react-redux hooks版本的计数器</h1>
      <div>{count}</div>
      <button onClick={()=>{increment(2)}}>+</button>
    </div>
  )
}
